<?php
/**
 * Created by IntelliJ IDEA.
 * User: FX
 * Date: 2017/12/22
 * Time: 10:26
 */
namespace common\config;
class TimeFormator
{
    /**
     * @return bool|string
     */
    public static function getCurrentFormatTime()
    {
        return date('Y-m-d H:i:s',time());
    }

    /**
     * @param $time
     * @return bool|string
     */
    public static function getFormatTime($time)
    {
        if(empty($time)){
            return '';
        }
        return date('Y-m-d H:i:s',$time);
    }

    /**
     * @param $time
     * @return bool|string
     */
    public static function getFormatDay($time)
    {
        if(empty($time)){
            return '';
        }
        return date('Y-m-d',$time);
    }

    /**
     * @param $time
     * @return bool|int
     */
    public static function getFormatTimestamp($time)
    {
        $timestamp = strtotime($time);
        return $timestamp>0?$timestamp:0;
    }

    /**
     * 两个时间间隔s 秒
     * @param $start
     * @param $end
     * @return int
     */
    public static function getDistant($start,$end)
    {
        if(empty($start)){
            return 0;
        }
        $start = strtotime($start);
        if(empty($end)){
            $end = self::getCurrentFormatTime();
        }
        $end = strtotime($end);
        if($end-$start<=0){
            return 0;
        }
        return $end-$start;
    }

    public static function getCurrentMicroTime()
    {
        return number_format(microtime(true)*1000,0,'.','');
    }

    public static function getMicroTime($time)
    {
        return $time*1000;
    }

    /**
     * 间隔 hh:mm:ss
     * @param $seconds
     * @return string
     */
    public static function getDistantBySeconds($seconds)
    {
        $h = intval($seconds / 3600);//间隔小时
        $m = intval(($seconds - $h* 3600 ) / 60);//间隔 分钟
        $s = $seconds - $h* 3600-$m*60;
        return sprintf('%s:%s:%s',self::formatTwoNumber($h),self::formatTwoNumber($m),self::formatTwoNumber($s));
    }

    /**
     * 数字保持2位
     * @param $n
     * @return string
     */
    public static function formatTwoNumber($n)
    {
        return $n>9 ? $n:sprintf('0%d',$n);
    }

}